﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;

namespace CodeGenerator.UI
{
    public partial class UcSelectTable : UserControl
    {
        public UcSelectTable()
        {
            InitializeComponent();
        }

        private IList<TableInfo> tableList;

        /// <summary>
        /// 返回选择的表
        /// </summary>
        public List<TableInfo> GetSelectTable
        {
            get
            {
                var select = new List<TableInfo>();
                if (lst_UseInfo.Items.Count > 0)
                {
                    for (int i = 0; i < lst_UseInfo.Items.Count; i++)
                    {
                        select.Add(tableList.Single(t => t.Name == lst_UseInfo.Items[i].ToString()));
                    }
                   
                }
                return select;
            }
        }

        private void UcSelectTable_Load(object sender, EventArgs e)
        {
            ServerInfo server = Constant.CurrentServerInfo;

            if (server != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {

                IDatabase database = DBFactory.Database(server.DBType);
                tableList = database.GetTableList(server, Constant.DatabaseName);
                foreach (var table in tableList)
                {
                    lst_Info.Items.Add(table.Name);
                }
            }
        }

        private void btn_RightMoveAll_Click(object sender, EventArgs e)
        {
            lst_UseInfo.Items.AddRange(lst_Info.Items);
            lst_Info.Items.Clear();
        }

        private void btn_LeftMoveAll_Click(object sender, EventArgs e)
        {
            lst_Info.Items.AddRange(lst_UseInfo.Items);
            lst_UseInfo.Items.Clear();
        }

        private void btn_RightMove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lst_Info.Items.Count; i++)
            {
                if (lst_Info.GetSelected(i))
                {
                    lst_UseInfo.Items.Add(lst_Info.Items[i]);
                    lst_Info.Items.RemoveAt(i);
                }
            }
        }

        private void btn_LeftMove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lst_UseInfo.Items.Count; i++)
            {
                if (lst_UseInfo.GetSelected(i))
                {
                    lst_Info.Items.Add(lst_UseInfo.Items[i]);
                    lst_UseInfo.Items.RemoveAt(i);
                }
            }
        }

        private void lst_Info_DoubleClick(object sender, EventArgs e)
        {
            btn_RightMove_Click(sender, e);
        }

        private void lst_UseInfo_DoubleClick(object sender, EventArgs e)
        {
            btn_LeftMove_Click(sender, e);
        }
    }
}
